package com.stu.controller;

import com.stu.commons.ServerResponse;
import com.stu.pojo.*;
import com.stu.service.*;
import com.stu.utils.JwtUtil;  // 导入新的 JwtUtil 工具类
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
public class SpikeProductController {

    @Autowired
    private ISpikeProductService iSpikeProductService;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IProductImgService iProductImgService;

    @Autowired
    private IProductDetailsService iProductDetailsService;

    @Autowired
    private IUsersService iUsersService;

    @Autowired
    private IAddressService iAddressService;

    @Autowired
    private ISpikeSessionsService iSpikeSessionsService;

    @Autowired
    private IMycouponService mycouponService;

    @PostMapping("spikeProduct")
    public SpikeProduct getSpikeProduct(String time){
        Integer sId = 0;
        if(time.contains("10"))
            sId = 1;
        else if(time.contains("12"))
            sId = 2;
        else if(time.contains("14"))
            sId = 3;
        else if(time.contains("16"))
            sId = 4;
        else if(time.contains("18"))
            sId = 5;
        SpikeProduct bySId = iSpikeProductService.getBySId(sId);
        return bySId;
    }

    @GetMapping("spikeProduct/detail")
    public ModelAndView getById(@RequestParam("proId") String proId){
        System.out.println("id消息发送到了后端："+proId);
        Integer spikeproductId = Integer.valueOf(proId);
        // 通过秒杀商品ID获取对应的秒杀场次
        SpikeProduct spikeProduct = iSpikeProductService.getById(spikeproductId);
        Integer productId1 = spikeProduct.getProductId();
        Integer spikeSessionsId = spikeProduct.getSpikeSessionsId();
        // 获取秒杀场次结束时间
        SpikeSessions spikeSessions = iSpikeSessionsService.selectSpikeSessions(spikeSessionsId);
        String spikeSessionsEndTime = spikeSessions.getSpikeSessionsEndTime();
        System.out.println("商品的ID："+productId1);
        ServerResponse user = iUsersService.getById(2);
        Product product = iProductService.getById(productId1);
        ProductImg productImg = iProductImgService.selectProductImg(productId1);
        ProductDetails productDetails = iProductDetailsService.selectProductDetailis(productId1);
        byte[] productDetail = productDetails.getProductDetail();
        // 编码转换 商品介绍展示
        String gbk=null;
        try {
            gbk = new String(productDetail, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        System.out.println(gbk);

        // 获取商品图片
        System.out.println(productImg);
        ModelAndView mav = new ModelAndView();
        mav.addObject("product",product);
        mav.addObject("user",user.getData());
        mav.addObject("productimg",productImg);
        mav.addObject("gbk",gbk);
        mav.setViewName("/product_show/detail.jsp");
        return mav;
    }

    @GetMapping("spikeProduct/goConfirm")
    public ModelAndView goConfirm(String proId, String token, HttpSession session){
        Integer productId = Integer.valueOf(proId);
        System.out.println("abcdefg:    "+proId+"以及productId:"+productId);
        SpikeProduct product = iSpikeProductService.getById(productId);

        // 使用新的JwtUtil解析token并获取用户id
        Claims claims = JwtUtil.parseToken(token);
        Integer custId = Integer.valueOf(claims.getSubject()); // 获取subject中的用户ID

        // 获取分布式锁，减少库存
        Boolean result = iSpikeProductService.decSpikeProductNumInRedis(productId);
        ModelAndView mav = new ModelAndView();
        if(result) {
            ServerResponse user = iUsersService.getById(custId);
            ServerResponse address = iAddressService.getAddressByUserId(custId);
            // 用户优惠券获取
            List<Mycoupon> myCouponsList = mycouponService.selectAll2Use(custId);
            mav.addObject("product", product);
            mav.addObject("user", user.getData());
            session.setAttribute("product", product);
            session.setAttribute("user", user.getData());
            session.setAttribute("addressList", address.getData());
            mav.addObject("addressList", address.getData());
            System.out.println(myCouponsList);
            mav.addObject("myCouponsList", myCouponsList);
            mav.setViewName("/personal/orderConfirm.jsp");
            return mav;
        } else {
            System.out.println("抢购商品失败，库存已不足！！！");
            mav.addObject("error", "抢购商品失败，库存已不足！！！");
            mav.setViewName("/index.jsp");
            return mav;
        }
    }
}
